<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        input[type="text"] {
            width: 120px;
            padding: 10px;
            margin-bottom: 20px;
        }
        button {
            width: 100px;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <h1>Calculator</h1>
    <form id="calculatorForm">
        <select id="api" name="api">
            <option value="plain">Servlet API</option>
            <option value="GET">REST GET</option>
            <option value="POST">REST POST</option>
            <option value="UPDATE">REST UPDATE</option>
            <option value="DELETE">REST DELETE</option>
        </select>
        <input type="text" id="x" placeholder="X">
        <select id="op" name="op">
            <option value="plus">+</option>
            <option value="subtract">-</option>
            <option value="multiply">*</option>
            <option value="divide">/</option>
        </select>
        <input type="text" id="y" placeholder="Y">
        <button type="submit">计算</button>
    </form>
    <div id="result"></div>

    <script>
        document.getElementById('calculatorForm').addEventListener('submit', function(event) {
            event.preventDefault();

            const x = parseFloat(document.getElementById('x').value);
            const y = parseFloat(document.getElementById('y').value);
            const op = document.getElementById('op').value;
            const api = document.getElementById('api').value;
            //Servlet使用的Query String风格的URL
            const url = `calculator?x=${x}&op=${op}&y=${y}`;
            //REST API使用的路径参数风格的URL
            var rest_url = `ws/${op}/${x}/${y}`;

            if(api == 'plain'){
            fetch(url)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('result').textContent = `Result: ${data}`;
                })
                .catch(error => {
                    console.error('Error:', error);
                    document.getElementById('result').textContent = 'Error occurred';
                });
            }else{
                fetch(rest_url, {method: `${api}`})
                .then(response => response.json())
                .then(result => {
                    if(result.code == 0){
                        document.getElementById('result').textContent = `Result: ${result.data}`;
                    }else{
                        document.getElementById('result').textContent = `Result: ${result.text}`;
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    document.getElementById('result').textContent = 'Error occurred';
                });
            }
        });
    </script>
</body>
</html>